package com.suyang.syt.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.suyang.syt.dao.interfaces.UserDao;
import com.suyang.syt.model.User;

@Repository
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

	@Override
	public long countUsername(String username) {
		String hql = " select count(*) from User u where u.username=? ";
		return (Long) this.queryByHql(hql, username);
	}

	@Override
	public long countEmail(String email) {
		String hql = " select count(*) from User u where u.email=? ";
		return (Long) this.queryByHql(hql, email);
	}

	@Override
	public long countPhone(String phone) {
		String hql = " select count(*) from User u where u.phone=? ";
		return (Long) this.queryByHql(hql, phone);
	}

	@Override
	public User loadByUsername(String username) {
		String hql = " from User u where u.username=? ";
		List<User> list = this.list(hql, username);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public boolean loadByPhone(String username) {
		String hql = " select count(*) from User u where u.username= ?";
		Long result = (Long) this.queryByHql(hql, username);
		if (result != 0) {
			return true;
		}
		return false;
	}

	@Override
	public void deletes(String ids) {
		String hql = " delete from User where id in(" + ids + ")";
		this.executeByHql(hql);
	}

	@Override
	public List<User> findAllUser() throws Exception {
		return this.list("from User ");
	}

}